package com.huangjue.backend.job;

import com.huangjue.backend.entity.ScheduleJob;
import com.huangjue.backend.mapper.HomeworkEvaluationMapper;
import com.huangjue.backend.mapper.ScheduleJobMapper;
import com.huangjue.backend.mapper.UserMapper;
import com.huangjue.backend.service.CallAgentService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Component
public class MyJob implements Job {

    private final ScheduleJobMapper scheduleJobMapper;
    private final CallAgentService callAgentService;
    private final HomeworkEvaluationMapper homeworkEvaluationMapper;
    private final UserMapper userMapper;


    @Value("${Agent.job.name}")
    private  String jobName;
    @Value("${Agent.job.groupName}")
    private String jobGroup;
    // 使用构造器注入
    public MyJob(ScheduleJobMapper scheduleJobMapper,
                 CallAgentService callAgentService,
                 HomeworkEvaluationMapper homeworkEvaluationMapper,
                 UserMapper userMapper) {
        this.scheduleJobMapper = scheduleJobMapper;
        this.callAgentService = callAgentService;
        this.homeworkEvaluationMapper = homeworkEvaluationMapper;
        this.userMapper = userMapper;
    }

    @Override
    public void execute(JobExecutionContext context) {
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();

        // 检查在昨天有提交作业记录的学生
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String todayStr = today.format(formatter);
        String yesterdayStr = yesterday.format(formatter);

//        System.out.println("当日日期：" + todayStr);       // 输出：当日日期：2025-10-17
//        System.out.println("前一天日期：" + yesterdayStr); // 输出：前一天日期：2025-10-16

        //查询在yesterdayStr,todayStr时间内有提交作业的用户的id值
        List<Integer> userIds;
        userIds = homeworkEvaluationMapper.selectByTime(yesterdayStr,todayStr );

        //根据id值去确定对应的username并给出对应的map
        if(userIds.size()!=0){
            List<Map<String, Object>> userNameList = userMapper.selectNicknameByIds(userIds);

            for (Map<String, Object> userNameMap : userNameList) {
                // 正确获取 nickname（String类型，直接强转）
                String nickname = (String) userNameMap.get("nickname");
                Integer userIdInt = (Integer) userNameMap.get("userId"); // 先拿到Integer

                //getAiReport内部已经做了异常时返回为0
                int flag = callAgentService.getAiReport(String.valueOf(userIdInt),nickname, yesterdayStr, todayStr);

                ScheduleJob scheduleJob = new ScheduleJob();
                scheduleJob.setConcurrent(0);
                scheduleJob.setJobName(jobName);
                scheduleJob.setJobGroup(jobDataMap.getString("croExpression"));
                scheduleJob.setStatus(flag);
                scheduleJob.setJobGroup(jobGroup);
                scheduleJob.setInvokeTarget("学生："+userIdInt+"在日期"+yesterdayStr+"-"+todayStr);
                scheduleJobMapper.insertJob(scheduleJob);
            }
        }else {
            System.out.println("没有查询到昨天提交了作业的同学");
        }
    }
}